package com.example.demo.controller;

/**
 * @author yanglup
 * @create 2020-11-22 15:12
 * 图片上传
 */

import ch.qos.logback.core.util.FileUtil;
import com.example.demo.pojo.Goodsaddguanli;
import com.github.pagehelper.PageInfo;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import java.util.logging.Logger;

@RestController
@RequestMapping("/upload")
public class UploadController {
    Logger logger= Logger.getLogger("获取值");

//    @RequestMapping("/picture")//每次只能完成一个图片转化
//    public String uploadPicture(HttpServletRequest request, HttpServletResponse response) throws Exception {
//
//        String filePath = "";//先置空等都弄好后拼接为目标的样式
//        request.setCharacterEncoding("utf-8"); //设置编码
////        String realPath = request.getSession().getServletContext().getRealPath("/uploadFile/");
//        String realPath = "E:/IDE/Myprograme/MyGraduate05/src/main/resources/static/img/";//我自己的文件
//        System.out.println(realPath);
//        File dir = new File(realPath);
//        //文件目录不存在，就创建一个
//        if (!dir.isDirectory()) {
//            dir.mkdirs();
//        }
//
//        try {
//            StandardMultipartHttpServletRequest req = (StandardMultipartHttpServletRequest) request;
//            //获取formdata的值
//            Iterator<String> iterator = req.getFileNames();//将获取到的小程序端的图片地址遍历
//            while (iterator.hasNext()) {
//                MultipartFile file = req.getFile(iterator.next());
//                String fileName = file.getOriginalFilename();
////                System.out.println("2.获取到的图片的fileName"+fileName);//就是微信端的图片去掉http://tmp/后的地址
//
//                //真正写到磁盘上
//                String uuid = UUID.randomUUID().toString().replace("-", "");
//                String kzm = fileName.substring(fileName.lastIndexOf("."));
//                String filename = uuid + kzm;
////                System.out.println("3.获取到的filename"+filename);//小的filename就是我们的图片拼接到本地后的
//
//                File file1 = new File(realPath + filename);
//                OutputStream out = new FileOutputStream(file1);
//                out.write(file.getBytes());
//                out.close();
////                将拼接起来-----------将这个返回回小程序端
////                System.out.println("4.request.getScheme()"+request.getScheme());//http
////                System.out.println("5.request.getServerName()"+request.getServerName());//localhost
////                端口
////                filename就我文件名xxx.png
//                filePath = request.getScheme() + "://" +
//                        request.getServerName()+ ":"
//                        + request.getServerPort()
//                        + "/img/" + filename;
//                System.out.println("访问图片路径:" + filePath);
//            }
//        } catch (Exception e) {
//            System.out.println("错误");
//        }
//        return filePath;
//    }


    @RequestMapping("/picture")
    public String uploadPicture(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String filePath = "";
        request.setCharacterEncoding("utf-8");

        String realPath = "D:/IDE/Myprograme/MyGraduate07/src/main/resources/static/img/";

//        String realPath = request.getSession().getServletContext().getRealPath("/uploadFile/");//放在c盘不然如果不重启访问不了
        System.out.println(realPath);
        File dir = new File(realPath);
        //文件目录不存在，就创建一个
        if (!dir.isDirectory()) {
            dir.mkdirs();
        }
        try {
            StandardMultipartHttpServletRequest req = (StandardMultipartHttpServletRequest) request;
            //获取formdata的值
            Iterator<String> iterator = req.getFileNames();

            while (iterator.hasNext()) {
                MultipartFile file = req.getFile(iterator.next());
                String fileName = file.getOriginalFilename();
                //真正写到磁盘上
                String uuid = UUID.randomUUID().toString().replace("-", "");
                String kzm = fileName.substring(fileName.lastIndexOf("."));
                String filename = uuid + kzm;
                File file1 = new File(realPath + filename);
                OutputStream out = new FileOutputStream(file1);
                out.write(file.getBytes());
                out.close();
                filePath = request.getScheme() + "://" +
                        request.getServerName() + ":"
                        + request.getServerPort()
                        + "/img/" + filename;
                System.out.println("访问图片路径:" + filePath);
            }
        } catch (Exception e) {
            System.out.println(e);
            System.out.println("错误");
        }
        return filePath;

    }
}








